package com.blocks.blocks;

import com.blocks.ScriptBlocksApplication;
import com.blocks.gui.BlockListGroup;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;

public class SmokeBlock extends Block {

	private ParticleEmitter smoke;
	private Material mat_red;
	
	public SmokeBlock() {	
		super();
	}
	public SmokeBlock(String name, Long masterId) {
		super(name, masterId);
		this.setBlockListGroup(BlockListGroup.GAME);
	}
	
	@Override
	public void init() {
		setTextureRow(1.0f);
		setTextureColumn(0.0f);
		 super.init();
		 initParticles();
	}
	private void initParticles()
	{
		smoke = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
	    mat_red = new Material(ScriptBlocksApplication.getInstance().getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");
	    
	    mat_red.setTexture("Texture", ScriptBlocksApplication.getInstance().getAssetManager().loadTexture("Effects/Smoke.png"));
	    smoke.setMaterial(mat_red);
	    smoke.setImagesX(1); smoke.setImagesY(1); // 2x2 texture animation
	    smoke.setInitialVelocity(new Vector3f(0, 2, 0));
	    smoke.setGravity(0);
	    smoke.setLowLife(0.5f);
	    smoke.setHighLife(3f);
	    smoke.setVelocityVariation(0.3f);
	    this.attachChild(smoke);
	    smoke.emitAllParticles();	    
	}
	
	@Override
	public void updateLogicalState(float tpf) {
		super.updateLogicalState(tpf);
		if( isPowered())
		{
			if(smoke == null)
			{
				initParticles();	
			}
			int freqValue = this.getFrequency(getBlockSides())!=0 ? this.getFrequency(getBlockSides()) : 10;
			this.getSmoke().setParticlesPerSec(freqValue*10);
			
		}
		else
		{
			if (smoke !=null)
			{
				this.detachChild(smoke);
				smoke = null;	
			}
			
		}
		
	}
	public ParticleEmitter getSmoke() {
		return smoke;
	}
	public void setSmoke(ParticleEmitter smoke) {
		this.smoke = smoke;
	}
	
	
}
